<template>
  <div class="list container">
    <header v-if="isShow">
      <div class="returns">
        <i class="iconfont icon-fanhui"></i>
      </div>
      <div class="search">
        <i class="iconfont icon-fangdajing"></i>
        <span>搜您喜欢的...</span>
      </div>
      <div class="go-home">
        <li class="iconfont icon-home"></li>
      </div>
    </header>
    <section>
      <div class="list-l" ref="left">
        <ul class="l-item">
          <li
            v-for="(item,index) in leftData"
            :key="index"
            @click="goScroll(index)"
            :class="{active:index == currentIndex}"
          >
            {{ item.name }}
          </li>
        </ul>
      </div>
      <div class="list-r" ref="right">
        <div>
          <ul class="shop-list" v-for="(item,index) in rightData" :key="index">
            <li v-for="(k,idx) in item" :key="idx">
              <h2>{{ k.name }}</h2>
              <ul class="r-content">
                <li v-for="(m,indx) in k.list" :key="indx">
                  <img v-lazy="m.imgUrl" alt="" />
                  <span>{{ m.name }}</span>
                </li>
              </ul>
            </li>
          </ul>
        </div>
      </div>
    </section>
    <tabbar class="tabbar"></tabbar>
  </div>
</template>

<script>
import Tabbar from "@/components/common/Tabbar.vue";
import Header from "@/components/home/Header.vue";

import BetterScroll from "better-scroll";
import http from "@/common/api/request";
export default {
  name: "List",
  data() {
    return {
      isShow:true,
      leftData: [], //左侧数据
      rightData: [], //右侧数据
      rightBScroll: "",
      allHeight: [],
      scrollY: "",
    };
  },
  components: {
    Tabbar,
    Header,
  },
  computed: {
    currentIndex(){
        return this.allHeight.findIndex((item,index)=>{
            return this.scrollY >= item && this.scrollY < this.allHeight[index+1]
        })
    }
  },
  async created() {
    let res = await http.$axios({
      url: "/api/goods/list",
    });
    // console.log(res);
    let leftArr = [];
    let rightArr = [];
    res.forEach((e) => {
      leftArr.push({
        id: e.id,
        name: e.name,
      });
      rightArr.push(e.data);
    });
    this.leftData = leftArr;
    this.rightData = rightArr;

    //当dom都加载完毕后再去执行，相当于window.onload
    this.$nextTick(() => {
      //左侧滑动
      new BetterScroll(this.$refs.left, {
         click: true,
         probeType: 3,
         bounce:false
      });
      //右侧滑动
      this.rightBScroll = new BetterScroll(this.$refs.right,{
         click: true,
         probeType: 3,
         bounce:false
      });
      //统计右侧高度值，并且放入数组中
      let height = 0;
      this.allHeight.push(height);
      let uls = this.$refs.right.getElementsByClassName("shop-list");
      //把dom对象转换成数组
      Array.from(uls).forEach((v) => {
        height += v.clientHeight;
        this.allHeight.push(height);
      });
      //得到右侧滚动的值
      this.rightBScroll.on("scroll", (pos) => {
        this.scrollY = Math.abs(pos.y);

        if ( Math.abs(pos.y) > 50){
          this.isShow = false
        }else{
          this.isShow = true
        }
      });
    });
  },
  methods: {
    goScroll(i) {
      this.rightBScroll.scrollTo(0, -this.allHeight[i], 300);
    },
  },
};
</script>

<style lang="scss" scoped>
.list {
  header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    height: 1.2571rem;
    color: #fff;
    background-color: #b0352f;
    .returns {
      line-height: 1.2571rem;
      padding: 0 0.5714rem;
      i {
        font-size: 0.5714rem;
      }
    }
    .search {
      display: flex;
      align-items: center;
      // width: 7rem;
      height: 0.8571rem;
      flex: 1;
      border-radius: 0.6857rem;
      background-color: #fff;
      i {
        color: #000;
        padding: 0 0.2857rem;
      }
      span {
        @extend i;
        font-size: 0.4rem;
      }
    }
    .go-home {
      @extend .returns;
      list-style: none;
    }
  }
  section {
    display: flex;
    flex: 1;
    overflow: hidden;
    .list-l {
      width: 2.6571rem;
      overflow: hidden;
      border-right: 1px solid #ccc;
      .l-item {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        li {
          width: 100%;
          // line-height: 1.2857rem;
          margin: .2286rem 0;
          padding: 0.1429rem 0;
          text-align: center;
          font-size: 0.4rem;
          &.active {
            color: #b54f4a;
            border-left: 5px solid #b54f4a;
          }
        }
      }
    }
    .list-r {
      flex: 1;
      overflow: hidden;
      .shop-list {
        text-align: center;
        h2 {
          padding: 0.2857rem 0;
          font-size: 0.5714rem;
          font-weight: 400;
        }
        .r-content {
          display: flex;
          flex-wrap: wrap;
          li {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            width: 33.3%;
            padding: 0.2857rem 0;
            img {
              width: 1.4857rem;
              height: 1.4857rem;
            }
            span {
              font-size: 0.4286rem;
            }
          }
        }
      }
    }
  }
  ::v-deep .tabbar {
    border-top: 1px solid #ccc;
  }
}
</style>
